<?php

class PaymentManager extends TModule
{
	// -------------------- basic functions --------------------
	public function addPayment($value, $description)
	{
		$payment = new PaymentRecord();
		$payment->setData(0, $value, $description);
		
		if($payment->save())
			return $payment;
		else
			return null;
	}
	
	public function deletePayment($id)
	{
		return PaymentRecord::finder()->deleteById($id);
	}
	
	public function updatePayment($id, $value, $description)
	{	
		$paymentRecord = new PaymentRecord();
		$paymentRecord->setData($id, $value, $description);
		return $this->updatePaymentByPaymentRecord($paymentRecord);
	}

	public function updatePaymentByPaymentRecord($paymentRecord)
	{	
		if($paymentRecord instanceof PaymentRecord)
		{
			$finder = PaymentRecord::finder();
			$id = $paymentRecord->getId();
			if($id > 0 && $rec = $finder->findByPk($id))
			{
				if($rec->equals($paymentRecord))
					return $rec;
				$rec->setDataByPaymentRecord($paymentRecord);
				if($rec->save())
					return $rec;
			}
		}
		return null;
	}
	
	public function getPaymentById($id)
	{
		return PaymentRecord::finder()->findById($id);
	}
	
	// -------------------- extended functions --------------------
	public function getPaymentsList($revert = false, $offset = -1, $limit = -1)
	{
		$criteria = new TActiveRecordCriteria();
		
		if($revert)
			;//$criteria->OrdersBy['registration'] = 'desc';
			
		if($offset < 0 || $offset == null)
			$offset = 0;
		if($limit < 0 || $limit == null)
			$limit = $this->countPayments();
			
		$criteria->Offset = $offset;
		$criteria->Limit = $limit;
		
		return PaymentRecord::finder()->findAll($criteria);
	}
	
	public function countPayments()
	{
		return PaymentRecord::finder()->count();
	}
}

?>